#include "serverconnection.h"

ServerConnection::ServerConnection(QHostAddress a, quint16 p, bool onlyReceive,
		Server*server) :
	ConnectionPlayer(a, p, onlyReceive)
{
	parentServer = server;
	score = 0;
}

ServerConnection::ServerConnection(bool onlyReceive, Server*server) :
	ConnectionPlayer(onlyReceive)
{
	parentServer = server;
}

Server* ServerConnection::server()
{
	return parentServer;
}

void ServerConnection::runCommand(PacketData* data, bool isNewestPacket)
{
	QString command = data->nextString();
	//qDebug() << "Client says: " << command;
	if (command == "HELLO")
		doHELLO(data);
	else if (command == "DISCONNECT")
		doDISCONNECT(data);
	else if (command == "STATE")
		doSTATE(data);
	else if (command == "DEAD")
		doDEAD(data);
	else if (command == "SHOOT")
		doSHOOT(data);
	//delete data;
}

void ServerConnection::doHELLO(PacketData* args)
{
	QString nick = args->nextString();
	setNick(nick);
	setScore(0);
	PacketData* toSend = new PacketData();
	toSend->appendString("HELLO");
	toSend->appendInt(getPlayerId());
	sendPacket(toSend, true);

	emit
	newPlayerConnected(this);
	connectionState = QAbstractSocket::ConnectedState;
	emit
	connectionStateChanged( connectionState);

	PacketData* toSend2 = new PacketData();
	toSend2->appendString("LIST");
	QHashIterator<QString, ServerConnection*> it(server()->getConnectionHash());
	while (it.hasNext())
	{
		it.next();
		ServerConnection* conn = it.value();
		if (conn != this)
		{
			toSend2->appendInt(conn->getPlayerId());
			toSend2->appendString(conn->getNick());
			toSend2->appendInt(conn->getScore());
		}
	}
	sendPacket(toSend2, true);

	PacketData* toSend3 = new PacketData();
	toSend3->appendString("JOIN");
	toSend3->appendInt(getPlayerId());
	toSend3->appendString(getNick());
	QVector<ServerConnection*> *except = new QVector<ServerConnection*> ();
	except->append(this);
	server()->sendToAll(toSend3, true, true, except);
	delete except;
}

void ServerConnection::doSTATE(PacketData* data)
{
	quint32 time = data->nextUInt();
	if (getPlayerTime() < time)
	{

		PlayerInput playerInput;

		quint8 dir1 = data->nextInt();
		quint8 dir2 = data->nextInt();
		qreal angle = data->nextReal();
		bool isShooting = data->nextInt()==1;
		//qDebug() << dir1 << dir2;
		playerInput.setDir1(dir1);
		playerInput.setDir2(dir2);
		playerInput.setAngle(angle);
		playerInput.setIsShooting(isShooting);

		QPointF oldPos = getPos();

		receiveInput(time, playerInput);

		PacketData* toSend = new PacketData();
		toSend->appendString("STATE");
		toSend->appendInt(getPlayerId());
		toSend->appendUInt(getPlayerTime());
		toSend->appendInt(getIsAlive());
		if (getIsAlive())
		{
			toSend->appendInt(dir[0]);
			toSend->appendInt(dir[1]);
			toSend->appendReal(getAngle());
			toSend->appendInt(getIsShooting());
			toSend->appendReal(oldPos.x());
			toSend->appendReal(oldPos.y());
		}

		//qDebug()<<"server: " << time << oldPos << getVelocity();

		server()->sendToAll(toSend, false, false);
	}
}

void ServerConnection::doDEAD(PacketData* data)
{
	PacketData* toSend = new PacketData();
	toSend->appendString("STATE");
	toSend->appendInt(getPlayerId());
	toSend->appendUInt(getPlayerTime());
	toSend->appendInt(getIsAlive());
	if (getIsAlive())
	{
		toSend->appendInt(dir[0]);
		toSend->appendInt(dir[1]);
		toSend->appendReal(getAngle());
		toSend->appendInt(getIsShooting());
		toSend->appendReal(getPos().x());
		toSend->appendReal(getPos().y());
	}

	sendPacket(toSend, false, false);
}

void ServerConnection::doSHOOT(PacketData* data)
{
	qreal x = data->nextReal();
	qreal y = data->nextReal();
	qint32 angle = data->nextInt();
	qDebug() << "remote player shoot";
	//fireGun(x,y,qreal(angle));
	PacketData* toSend = new PacketData();
	toSend->appendString("SHOOT");
	toSend->appendInt(getPlayerId());
	toSend->appendReal(x);
	toSend->appendReal(y);
	toSend->appendInt(angle);
	QVector<ServerConnection*> *except = new QVector<ServerConnection*> ();
	except->append(this);

	server()->sendToAll(toSend, true, false, except);
	delete except;
}

void ServerConnection::doDISCONNECT(PacketData* args)
{
	disconnFromHost();
}
